package ie.dkit.java3Demulation.gui;

import ie.dkit.java3Demulation.objects.GraphicsObject;

import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class GraphicsObjectTableModel<E extends GraphicsObject> extends
		AbstractTableModel implements Collection<E> {
	/**
	 * 
	 */
	private static final long serialVersionUID = -9196573116498177771L;

	private Vector<E> vector = new Vector<E>();

	private static final String[] columnNames = new String[] { "Type", "Name", };

	private static final Class[] columnTypes = new Class[] { String.class,
			String.class };

	public static final boolean[] editableTypes = new boolean[] { false, true };

	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return vector.size();
	}

	public Class<?> getColumnClass(int columnIndex) {
		return columnTypes[columnIndex];
	}

	public String getColumnName(int column) {
		return columnNames[column];
	}

	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return editableTypes[columnIndex];
	}

	@SuppressWarnings("unchecked")
	public Object getValueAt(final int rowIndex, final int columnIndex) {
		switch (columnIndex) {
		case 0:
			return vector.get(rowIndex).getClass().getSimpleName();
		case 1:
			return vector.get(rowIndex).getName();
		default:
			break;
		}
		return null;
	}

	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		if (editableTypes[columnIndex]) {
			switch (columnIndex) {
			case 0:
				break;
			case 1:
				getGraphicsObjectAt(rowIndex).setName((String) aValue);
				fireTableCellUpdated(rowIndex, columnIndex);
				break;
			default:
				break;
			}
		}
	}

	public GraphicsObject getGraphicsObjectAt(int index) {
		return vector.get(index);
	}

	public boolean add(E e) {
		return vector.add(e);
	}

	public boolean addAll(Collection<? extends E> c) {
		return vector.addAll(c);
	}

	public void clear() {
		vector.clear();
	}

	public boolean contains(Object o) {
		return vector.contains(o);
	}

	public boolean containsAll(Collection<?> c) {
		return vector.containsAll(c);
	}

	public boolean isEmpty() {
		return vector.isEmpty();
	}

	public Iterator<E> iterator() {
		return vector.iterator();
	}

	public boolean remove(Object o) {
		return vector.remove(o);
	}

	public boolean removeAll(Collection<?> c) {
		return vector.removeAll(c);
	}

	public boolean retainAll(Collection<?> c) {
		return vector.retainAll(c);
	}

	public int size() {
		return vector.size();
	}

	public Object[] toArray() {
		return vector.toArray();
	}

	public <T> T[] toArray(T[] a) {
		return vector.toArray(a);
	}
}
